var bookinfo = undefined;
var webinfo = undefined;
var prevpage = null; //上一章
var listpage = null; //章节目录
var nextpage = null; //下一章

mui.plusReady(function() {
	// 初始化页面
	var self = plus.webview.currentWebview();
	bookinfo = self.Bmarker;
	webinfo = self.Wmarker;
	$("#index").html(bookinfo.title_name);
	load(bookinfo.url_page, "new");
});

mui('.reader-tab').on('tap', '.reader-tab-item', function() {
	if (this.getAttribute("id") == "prevpage") {
		load(prevpage, "new");
	}
	if (this.getAttribute("id") == "index") {
		getIndex();
	}
	if (this.getAttribute("id") == "nextpage") {
		load(nextpage, "new");
	}
});

function load(page, flag) {
	if (!RegExp(webinfo.url_page).test(page)) {
		if (flag == "new") mui.toast("章节不存在！");
		return 0;
	}
	if (flag == "next") mui("body").off("swipe", ".page-content", swiper);
	if (flag == "new") plus.nativeUI.showWaiting();
	$(".mui-icon").css("top", "1px");
	mui.ajax({
		url: page,
		timeout: 5000,
		dataType: 'html',
		success: function(response) {
			bookinfo.url_page = page;
			var BookCache = $("<code></code>").append($(response));
			if (webinfo.patch_content != "") {
				eval(webinfo.patch_content);
			}
			prevpage = BookCache.find(webinfo.s_pageprev).attr("href");
			listpage = BookCache.find(webinfo.s_pagelist).attr("href");
			nextpage = BookCache.find(webinfo.s_pagenext).attr("href");
			prevpage = getAbsolute(page, prevpage);
			listpage = getAbsolute(page, listpage);
			nextpage = getAbsolute(page, nextpage);
			bookinfo.title_mark = BookCache.find(webinfo.title_mark).html();
			$("#title").html(bookinfo.title_mark);
			$('#context').html(BookCache.find(webinfo.s_content).html());
			$('.book-content').scrollTop(0);
			
			if (bookinfo.title_mark == bookinfo.title_last) { //判断书签是否需要检索更新
				bookinfo.title_last = "待更新";
			}
			if (!RegExp(webinfo.url_page).test(nextpage)) {
				bookinfo.title_last = bookinfo.title_mark;
			}

			$(".mui-icon").css("top", "-100px");
			if (flag == "next") mui("body").on("swipe", ".page-content", swiper);
			if (flag == "new") plus.nativeUI.closeWaiting();
			
			setNewMarker();
		},
		error: function(result) {
			$('#title').text('...failed...'); 
			mui.toast("内容加载失败！");
			$(".mui-icon").css("top", "-100px");
			if (flag == "next") mui("body").on("swipe", ".page-content", swiper);
			if (flag == "new") plus.nativeUI.closeWaiting();
		}
	});
};

function getIndex() {
	if (listpage) {
		plus.nativeUI.showWaiting("目录加载中...");
		mui.ajax({
			url: listpage,
			timeout: 5000,
			dataType: 'html',
			success: function(response) {
				var BookIndex = $("<code></code>").append($(response));
				$("#titlelist").empty();
				var titlelink = BookIndex.find(webinfo.title_list);
				var titlelist = $('<ul></ul>');
				titlelink.each(function() {
					titlelist.append('<li class="mui-table-view-cell"></li>');
					titlelist.find("li:last").append(this);
				});
				plus.nativeUI.closeWaiting();
				
				mui.openWindow({
					url: "bookindex.html",
					id: "bookindex",
					extras: {
						bookindex: titlelist.html()
					}
				});
			},
			error: function(result) {
				plus.nativeUI.closeWaiting();
			}
		});
	}
}

mui("body").on("swipe", ".page-content", swiper);

function swiper() {
	//	if ($(document).scrollTop() < 10) {
	//		prevload(lastpage); //预加载上一章
	//	}
	//	if ($("div.page-content div:last").offset().top - $(document).scrollTop() < $(window).height()) {
	//		load(nextpage, "next"); //预加载下一章
	//	}
};

function setNewMarker() {
	var homePage = plus.webview.currentWebview().opener();
	mui.fire(homePage, 'setbookmarker', bookinfo);
}

//从回调参数打开章节
window.addEventListener('openbook', function(event) {
	var openlink = getAbsolute(listpage,event.detail);
	if (!RegExp(webinfo.url_page).test(openlink)) {
		if (flag == "new") mui.toast("章节不存在！");
		return;
	} else {
		mui('.mui-popover').popover('hide');
		load(openlink, "new");
	}
});

//相对路径转绝对路径
function getAbsolute(page, link) {
	webindex = page.replace(/(http....*?)\/.*/, "$1"); //当前页面的主目录
	thisparent = page.replace(/(.*\/).*$/, "$1"); //当前页面的父目录
	if (link.indexOf("http") == 0) return link;
	if (link.indexOf("/") == 0) return (webindex + link);
	return (thisparent + link);
}